﻿using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Caliburn.Micro;
using SverigesRadio.SverigesRadioApp.Business;
using SverigesRadio.SverigesRadioApp.Business.Model;

namespace SverigesRadio.SverigesRadioApp.WindowsPhoneApplication.ViewModels.Programs
{

    public class ProgramsPageViewModel : NavigatableViewModelBase
    {
        public const string CategoryIdQueryString = "CategoryId";
        public const string TitleText_ArchivedPrograms = "Arkiverade program";

        private IServiceCaller<CategoryPrograms> programsService;



        private string programCategoryId;

        public ProgramsPageViewModel(INavigationService navigationService, IDataContext dataContext) : base(navigationService, dataContext)
        {
            programsService = DataContext.GetProgramsServiceCaller();
            programsService.OnServiceCallReceived += OnServiceCallReceived;
        }

        public string ProgramCategoryId
        {
            get { return programCategoryId; }
            set { programCategoryId = value; }
        }

        private IList<Group<Program>> programs;
        public IList<Group<Program>> Programs
        {
            get { return programs; }
            set
            {
                programs = value;
                NotifyOfPropertyChange("Programs");
                NotifyOfPropertyChange("SelectedProgram");
            }
        }

        private Program selectedProgram;
        public Program SelectedProgram
        {
            get { return selectedProgram; }
            set
            {
                selectedProgram = value;

                if (value == null)
                    return;

                NavigationService.UriFor<ProgramPageViewModel>().WithParam(p => p.ProgramCategoryTitle,
                                                                           CategoryTitleText).
                    WithParam(p => p.ProgramTitle, value.Title).WithParam(p => p.PoddId, value.PodId).Navigate();
            }
        }

        private string categoryTitleText;
        public string CategoryTitleText
        {
            get { return categoryTitleText; }
            set
            {
                categoryTitleText = value;
                NotifyOfPropertyChange("CategoryTitleText");
            }
        }

        protected override void OnViewLoaded(object view)
        {
            base.OnViewLoaded(view);

            IsBusy = true;

            if (string.IsNullOrEmpty(ProgramCategoryId))
                programsService.CallAsync(SverigesRadioServices.CategoryPrograms);
            else
                programsService.CallAsync(SverigesRadioServices.CategoryPrograms, CategoryIdQueryString + "=" + ProgramCategoryId);

        }


        void OnServiceCallReceived(object sender, ServiceCallerEventArgs<CategoryPrograms> e)
        {
            IsBusy = false;

            if (e.Error != null)
                throw e.Error;


            if (CategoryTitleText.Equals(TitleText_ArchivedPrograms, System.StringComparison.InvariantCultureIgnoreCase))
            {
                Programs = (from program in e.ServiceResult.Programs
                            where program.Archived
                            orderby program.FirstCapital
                            group program by program.FirstCapital
                                into fc
                                orderby fc.Key
                                select new Group<Program>(fc.Key.ToString(), fc)).ToList();
            }
            else
            {
                Programs = (from program in e.ServiceResult.Programs
                            where program.Archived == false
                            orderby program.FirstCapital
                            group program by program.FirstCapital
                                into fc
                                orderby fc.Key
                                select new Group<Program>(fc.Key.ToString(), fc)).ToList();
            }


        }

    }

}
